﻿using System;
using System.Windows.Forms;
using GuidanceExplorer.Model;
using GuidanceExplorer.Services;
using GuidanceExplorer.Views;

namespace GuidanceExplorerAddIn.Views
{
    public partial class GuidanceItemDetailView : UserControl//: BaseDetailView
    {
        #region Dependencies Properties

        /// <summary>
        /// The HTML rendering service for the guidance items.
        /// </summary>
        public IHtmlRenderService RenderService { set; protected get; }

        /// <summary>
        /// The main model service instance.
        /// </summary>
        public IModelStorageService ModelService { set; protected get; }

        public GEController Controller { set; protected get; }

        #endregion

        public GuidanceItemDetailView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Show the details of the item in the details pane.
        /// </summary>
        public void ShowDetails(GuidanceItemFileReference fileReference)
        {
            if (fileReference != null)
            {
                webBrowser.DocumentStream = RenderService.GetDocumentStreamForShowing(fileReference);
            }
            else
            {
                //Clean the contents.
                webBrowser.DocumentStream = null;
            }
        }
        
        /// <summary>
        /// Occurs when the browser is navigating to an address.
        /// </summary>
        protected void webBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            WebBrowser browser = (WebBrowser)sender;
            GuidanceItem guidanceItem = null;
            if (e.Url.Scheme == Uri.UriSchemeHttp)
            {
                e.Cancel = true;
                browser.Navigate(e.Url, true);
            }
            else if (e.Url.Scheme == "ruleid")
            {
                e.Cancel = true;
                string ruleId = e.Url.AbsolutePath;
                guidanceItem = ModelService.FindItemById(ruleId);
                if (guidanceItem != null)
                {
                    GuidanceItemFileReference reference = guidanceItem as GuidanceItemFileReference;
                    if (reference != null && browser != null)
                    {
                        browser.DocumentStream = RenderService.GetReadOnlyDocumentStream(reference);
                    }
                }
            }
            else if (e.Url.Scheme == "ruledisplay")
            {
                e.Cancel = true;
                string ruleId = e.Url.AbsolutePath;
                guidanceItem = ModelService.Guidance.Items.FindItemById(ruleId);
                GuidanceItemFileReference gifr = ModelService.Guidance.GetGuidanceItemFileReference(guidanceItem);

                Controller.GuidanceItemOpened(this, new GuidanceItemFileReferenceEventArgs(new GuidanceItemFileReference[] { gifr }, ReadProtection.ReadOnly));
            }
        }
    }
}
